package com.easidea.tim.service.customer;


import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springside.core.service.EntityService;

import com.easidea.tim.dao.customer.CustomerTypeDao;
import com.easidea.tim.entity.customer.CustomerType;
import com.easidea.tim.util.Page;

public class CustomerTypeService extends EntityService<CustomerType, CustomerTypeDao> {

	private CustomerTypeDao customerTypeDao;
	
	@SuppressWarnings("unchecked")
	public Page getCustomerTypeListByCondition(String name, Page page) {
		 Criteria ca = getCustomerTypeListByConditionCriteria(name);
		 ca.setProjection(Projections.count("name"));
		 Integer totalNum = (Integer)ca.uniqueResult();
		 Criteria ca1 = getCustomerTypeListByConditionCriteria(name);
		 int startIndex = Page.getStartOfPage(page.getCurrentPageNo(), page.getPageSize());
		 ca1.setFirstResult(startIndex);
		 ca1.setMaxResults(page.getPageSize());
		 List<CustomerType> ls = (List<CustomerType>)ca1.list();
		 Page resultPage = new Page(startIndex, totalNum, page.getPageSize(), ls);
		 resultPage.setCurrentPageNo(page.getCurrentPageNo());				
		return resultPage;

	}
	private Criteria getCustomerTypeListByConditionCriteria(String name) {
		Criteria ca = this.createUnDeletedCriteria();
		if(name != null && !name.equals("")) {
			ca.add(Restrictions.ilike("name", name, MatchMode.START));
		}		
		return ca;
	}
	@SuppressWarnings("unchecked")
	public List<CustomerType> getAllCustomerType() {
		Criteria ca = createUnDeletedCriteria();
		return ca.list();
	}
	public CustomerType getCustomerTypeById(Long id) {
		CustomerType type = (CustomerType) customerTypeDao.getHibernateTemplate().get(CustomerType.class, id);
		return type;
	}
	private boolean hasSameName(long id, String name) {
		boolean ret = false;
		Criteria ca = createUnDeletedCriteria();
		if(name != null && !name.equals("")) {
			ca.add(Restrictions.eq("name", name));
		}
		if(id != -1) {
			ca.add(Restrictions.ne("id", id));
		}
		if(ca.uniqueResult() != null) {
			ret = true;
		}
		return ret;
	}
	public String saveOrUpdateCustomerType(CustomerType type) {
		if(hasSameName(type.getId(), type.getName())) {
			return "ExistSameName";
		}
		try{		
			if(type.getId() == -1) {
				customerTypeDao.getHibernateTemplate().save(type);
			}else {
				customerTypeDao.getHibernateTemplate().update(type);
			}
		}catch(DataAccessException de) {
			de.printStackTrace();
			return "failure";
		}
		return "success";
	}
	public boolean checkDelCustomerType(Long id) {
		return customerTypeDao.checkDelCustomerType(id); 
	}
	public boolean deleteCustomer(Long id){		
	    CustomerType  type = getCustomerTypeById(id);
	    type.setDeleted((short)1);
	    type.setModifyTime(new Date());
		customerTypeDao.getHibernateTemplate().update(type);
		return true;
	}
	
	@Override
	public CustomerTypeDao getEntityDao() {
		// TODO Auto-generated method stub
		return customerTypeDao;
	}
	public CustomerTypeDao getCustomerTypeDao() {
		return customerTypeDao;
	}
	public void setCustomerTypeDao(CustomerTypeDao customerTypeDao) {
		this.customerTypeDao = customerTypeDao;
	}
 
}
